package com.zzzy.doc.controller.processdoc;

import com.zzzy.common.util.FileUtil;
import com.zzzy.common.vo.AjaxResult;
import com.zzzy.doc.dao.entity.DocAdvice;
import com.zzzy.doc.dao.entity.Document;
import com.zzzy.doc.service.DocAdviceService;
import com.zzzy.doc.service.processdoc.ProcessDocService;
import com.zzzy.doc.vo.document.DocBasicInfoRes;
import com.zzzy.doc.vo.document.DocInfoRes;
import com.zzzy.doc.vo.document.process.ProcessDocStatisticsRes;
import com.zzzy.doc.vo.pageoffice.OpenOfficeFileRes;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.List;

@Tag(name = "办理公文", description = "doc200")
@Validated
@RestController
@RequestMapping("/doc/process")
public class ProcessDocController {

    @Resource
    private ProcessDocService processDocService;
    @Resource
    private DocAdviceService docAdviceService;

    @GetMapping("/getDocBasicInfo/{id}")
    @Operation(summary = "获取公文基本信息")
    @Parameter(name = "id", description = "公文ID", required = true)
    @PreAuthorize("@pvs.hasPermission('doc:process:page')")
    public AjaxResult<DocBasicInfoRes> getDocBasicInfo(@NotNull(message = "公文ID不能为空") @PathVariable("id") Long id) {
        DocBasicInfoRes doc = processDocService.getDocBasicInfo(id);
        return AjaxResult.success(doc);
    }

    @GetMapping(value = "/getApprovalPageOfficeFile/{id}")
    @Operation(summary = "打开呈批表文件")
    @Parameter(name = "id", description = "公文ID", required = true)
    @PreAuthorize("@pvs.hasPermission('doc:process:page')")
    public AjaxResult<OpenOfficeFileRes> getApprovalPageOfficeFile(@NotNull(message = "公文ID不能为空") @PathVariable("id") Long id) {
        String openFileUrl = "/doc/process/getApprovalDocFileStream/" + id;
        OpenOfficeFileRes openOfficeFileRes = processDocService.getApprovalPageOfficeResByDocId(id, openFileUrl);
        return AjaxResult.success(openOfficeFileRes);
    }

    @GetMapping(value = "/getApprovalDocFileStream/{id}")
    @Operation(summary = "获取呈批表文件流")
    @Parameter(name = "id", description = "公文ID", required = true)
    @PreAuthorize("@pvs.hasPermission('doc:process:page')")
    public void getApprovalDocFileStream(@NotNull(message = "公文ID不能为空") @PathVariable("id") Long id, HttpServletResponse response) {
        DocInfoRes docInfo = processDocService.getDocInfo(id);
        byte[] fileBlob = docInfo.getApprovalFileBlob();
        FileUtil.outputFileStream(response, fileBlob, docInfo.getSubject() + "." + FileUtil.getFileType(fileBlob));
    }

    @GetMapping(value = "/getMainPageOfficeFile/{id}")
    @Operation(summary = "打开正文文件")
    @Parameter(name = "id", description = "公文ID", required = true)
    @PreAuthorize("@pvs.hasPermission('doc:process:page')")
    public AjaxResult<OpenOfficeFileRes> getMainPageOfficeFile(@NotNull(message = "公文ID不能为空") @PathVariable("id") Long id) {
        String openFileUrl = "/doc/process/getMainDocFileStream/" + id;
        OpenOfficeFileRes openOfficeFileRes = processDocService.getMainPageOfficeResByDocId(id, openFileUrl);
        return AjaxResult.success(openOfficeFileRes);
    }

    @GetMapping(value = "/getMainDocFileStream/{id}")
    @Operation(summary = "获取正文文件流")
    @Parameter(name = "id", description = "公文ID", required = true)
    @PreAuthorize("@pvs.hasPermission('doc:process:page')")
    public void getMainDocFileStream(@NotNull(message = "公文ID不能为空") @PathVariable("id") Long id, HttpServletResponse response) {
        DocInfoRes docInfo = processDocService.getDocInfo(id);
        byte[] fileBlob = docInfo.getMainFileBlob();
        FileUtil.outputFileStream(response, fileBlob, docInfo.getSubject() + "." + FileUtil.getFileType(fileBlob));
    }

    @GetMapping("/getDocImage/{id}")
    @Operation(summary = "获取公文正文第一页缩略图")
    @Parameter(name = "id", description = "公文ID", required = true)
    @PreAuthorize("@pvs.hasPermission('doc:process:page')")
    public void getDocImage(@NotNull(message = "公文ID不能为空") @PathVariable("id") Long id, HttpServletResponse response) {
        Document doc = processDocService.getDocByIdForQuery(id);
        FileUtil.outputFileStream(response, doc.getDocThumbnailBlob(), doc.getSubject());
    }

    @Operation(summary = "意见汇总列表列表")
    @GetMapping(value = "/getAdviceList/{id}")
    @Parameter(name = "id", description = "公文ID", required = true)
    @PreAuthorize("@pvs.hasPermission('doc:process:page')")
    public AjaxResult<List<DocAdvice>> getAdviceList(@NotNull(message = "公文ID不能为空") @PathVariable("id") Long id) {
        List<DocAdvice> adviceList = docAdviceService.getAdviceListDocId(id);
        return AjaxResult.success(adviceList);
    }
    @GetMapping(value = "/getFinalPageOfficeFile/{id}")
    @Operation(summary = "打开最终文件（合并后的pdf）")
    @Parameter(name = "id", description = "公文ID", required = true)
    @PreAuthorize("@pvs.hasPermission('doc:process:page')")
    public AjaxResult<OpenOfficeFileRes> getFinalPageOfficeFile(@NotNull(message = "公文ID不能为空") @PathVariable("id") Long id) {
        String openFileUrl = "/doc/my/getFinalDocFileStream/" + id;
        OpenOfficeFileRes openOfficeFileRes = processDocService.getFinalPageOfficeResByDocId(id, openFileUrl);
        return AjaxResult.success(openOfficeFileRes);
    }

    @GetMapping(value = "/getFinalDocFileStream/{id}")
    @Operation(summary = "获取最终文件流（合并后的pdf）")
    @Parameter(name = "id", description = "公文ID", required = true)
    @PreAuthorize("@pvs.hasPermission('doc:process:page')")
    public void getFinalDocFileStream(@NotNull(message = "公文ID不能为空") @PathVariable("id") Long id, HttpServletResponse response) {
        DocInfoRes docInfo = processDocService.getDocInfo(id);
        byte[] fileBlob = docInfo.getPdfFileBlob();
        FileUtil.outputFileStream(response, fileBlob, docInfo.getSubject() + "." + FileUtil.PDF);
    }

    @Operation(summary = "统计信息")
    @GetMapping(value = "/getStatistics")
    @PreAuthorize("@pvs.hasPermission('doc:process:page')")
    public AjaxResult<ProcessDocStatisticsRes> getStatistics() {
        ProcessDocStatisticsRes statistics = processDocService.getStatistics();
        return AjaxResult.success(statistics);
    }
}
